package com.wing.wechat;

import com.alibaba.fastjson.JSONObject;
import com.wing.utils.HttpsUtils;
import com.wing.wechat.util.WechatUtil;
import com.wing.wechat.vo.*;
import org.springframework.stereotype.Service;

/**
 * 企业微信打卡服务实现
 */
@Service
public class WechatCheckinServiceImpl implements WechatCheckinService {

    @Override
    public ResponseToken getAccessToken(String secret) {
        String requestUrl = WechatUtil.GET_TOKEN_URL.replace("REQUEST_ID", WechatUtil.ENTERPRISE_ID).replace("REQUEST_SECRET", secret);
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseToken responseToken = JSONObject.parseObject(result, ResponseToken.class);
        return responseToken;
    }

    @Override
    public ResponseCorpCheckinGroup getCorpCheckinOption() {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.GET_ENTERPRISE_CLOCK_RULE_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, null);
        ResponseCorpCheckinGroup userCheckGroup = JSONObject.parseObject(result, ResponseCorpCheckinGroup.class);
        return userCheckGroup;
    }

    @Override
    public ResponseCheckinData getCheckinData(RequestCheckinData checkinData) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.GET_CHECKIN_DATA_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(checkinData));
        ResponseCheckinData responseCheckinData = JSONObject.parseObject(result, ResponseCheckinData.class);
        return responseCheckinData;
    }

    @Override
    public ResponseCheckinMonthData getCheckinMonthData(RequestCheckinMonthData checkinMonthData) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.GET_CHECKIN_MONTH_DATA_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(checkinMonthData));
        ResponseCheckinMonthData responseCheckinMonthData = JSONObject.parseObject(result, ResponseCheckinMonthData.class);
        return responseCheckinMonthData;
    }

    @Override
    public ResponseCheckinSchedule getCheckinScheduleList(RequestCheckinSchedule checkinSchedule) {
        ResponseToken token = getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        String requestUrl = WechatUtil.GET_CHECKIN_SCHEDULE_LIST_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(checkinSchedule));
        ResponseCheckinSchedule responseCheckinSchedule = JSONObject.parseObject(result, ResponseCheckinSchedule.class);
        return responseCheckinSchedule;
    }

}
